package org.demo.cnck.controller.vm;

import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * @Author: LiaoYouLe
 * @Description:
 * @Date: Created in 15:39 2018/3/14
 * @Modified By:
 */
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public class MessageHelper implements HttpMessage {

    private ZoneStatus status;
    private String message;

    /**
     * 正常
     *
     * @return
     */
    public static ZoneMessageBuilder OK() {
        return builder(ZoneStatus.OK);
    }

    /**
     * 错误的请求
     *
     * @return
     */
    public static ZoneMessageBuilder BAD_REQUEST() {
        return builder(ZoneStatus.BAD_REQUEST);
    }

    /**
     * 内部请求错误
     *
     * @return
     */
    public static ZoneMessageBuilder INTER_ERROR() {
        return builder(ZoneStatus.INTERNAL_SERVER_ERROR);
    }

    @Override
    public int getCode() {
        return this.status.code;
    }

    @Override
    public String getMessage() {
        return this.message;
    }

    private MessageHelper(ZoneStatus status, String message) {
        this.status = status;
        this.message = message;
    }

    private static MessageHelper.ZoneMessageBuilder builder(ZoneStatus status) {
        return new MessageHelper.ZoneMessageBuilder(status);
    }

    public static class ZoneMessageBuilder {
        private ZoneStatus status;
        private String message;

        ZoneMessageBuilder(ZoneStatus status) {
            this.status = status;
        }


        public MessageHelper.ZoneMessageBuilder message(String message, Object... params) {
            this.message = String.format(message, params);
            return this;
        }

        public MessageHelper build() {
            return new MessageHelper(this.status, this.message);
        }

    }

}
